package com.hzqy.service.statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.statistics.CustomerOnDemandListenDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.service.pms.PmsServiceBean;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.statistics.UserListenHistoryVo;

@Service
public class CustomerOnDemandListenServiceBean extends GenericServiceBean<PmsUserVo, CustomerOnDemandListenDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private CustomerOnDemandListenDao customerOnDemandListenDao;
	
	@Override
	public CustomerOnDemandListenDao getEntityDao() {
		return customerOnDemandListenDao;
	}
	@Resource
	private PmsServiceBean pmsServiceBean;
	
	
	
	/**
	 * 初始化查询时查询当前账号下的所有客户并根据客户分组
	 * @param userVo
	 * @param UVo
	 * @return
	 */
	public Map<String, Object> selectListenHistoryCustomerOnDemandList(PmsUserVo userVo,UserListenHistoryVo UVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		Map<String,Object> serviceMap = new HashMap<String, Object>();
		Map<String,Object> contentMap = new HashMap<String, Object>();
		
		String designatedOrgs = SystemConfigUtils.getSysConfig("NBDiseaseControl_"+userVo.getOrgId());
		if(designatedOrgs!=null && !"".equals(designatedOrgs)){
			map.put("orgIds",designatedOrgs);
			
			serviceMap.put("orgIds",designatedOrgs);
			contentMap.put("orgIds",designatedOrgs);
		}else{
			if(userVo.getParentOrgId() > 0){
				String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
				map.put("orgIds",orgIds);
				
				serviceMap.put("orgIds",orgIds);
				contentMap.put("orgIds",orgIds);
			}
		}
		
		map.put("beginDate", UVo.getBeginDate());
		map.put("endDate", UVo.getEndDate());
		map.put("statistical_dimension","po.f_po_id");
		
		serviceMap.put("beginDate", UVo.getBeginDate());
		contentMap.put("beginDate", UVo.getBeginDate());
		serviceMap.put("endDate", UVo.getEndDate());
		contentMap.put("endDate", UVo.getEndDate());
		serviceMap.put("statistical_dimension","ccs.f_cc_searchname");
		contentMap.put("statistical_dimension","ulh.f_ulh_contentid");
		
		
		Integer rowCount = customerOnDemandListenDao.selectListenHistoryCustomerOnDemandByStatisticalDimensionCount(map);
		
		map.put("customerTotal", rowCount);
		if(rowCount > 0) {
			//根据客户分组查询并统计每个客户的总时间放在左侧使用
			List<UserListenHistoryVo> results = customerOnDemandListenDao.selectListenHistoryCustomerOnDemandByStatisticalDimensionList(map);
			for(int i=0;i<results.size();i++){
				String  cod=results.get(i).getF_ulh_playtime();
				int code =0;
				boolean flag=true;
				for(int k=0;k<cod.length();k++){
					if (!Character.isDigit(cod.charAt(k))){
						flag=false;
					}
				}
				
				if(flag && !cod.equals("")){
					code =Integer.parseInt(results.get(i).getF_ulh_playtime());
				}else{
					code=0;
				}
				UserListenHistoryVo uv=results.get(i);
				uv.setF_ulh_play_minut((code/60)+"");
				uv.setF_ulh_playtime(code+"");
				int h=code/3600;
				int m=(code-3600*h)/60;
				int s=code%60;
				String strh="";
				String strm="";
				String strs="";
				if(h==0){
					strh="0";
				}else{
					strh=""+h;
				}
				if(m==0){
					strm="0";
				}else{
					strm=""+m;
				}
				if(s==0){
					strs="0";
				}else{
					strs=""+s;
				}
				
				uv.setF_ulh_play_hms(strh+"小时"+strm+"分"+strs+"秒");
				
				results.set(i,uv );
			}
			map.put("companyRows", results);
			
			//根据第一个客户查询该客户下的业务账号并统计每个业务账号下的对应的时间
			if(UVo.getF_cc_companyid()==null||"".equals(UVo.getF_cc_companyid())){
				serviceMap.put("f_cc_companyid", results.get(0).getF_cc_companyid());
				contentMap.put("f_cc_companyid", results.get(0).getF_cc_companyid());
			}else{
				serviceMap.put("f_cc_companyid", UVo.getF_cc_companyid());
				contentMap.put("f_cc_companyid", UVo.getF_cc_companyid());
			}
			
			
			Integer serviceRowCount = customerOnDemandListenDao.selectListenHistoryCustomerOnDemandByStatisticalDimensionCount(serviceMap);
			map.put("serviceTotal", serviceRowCount);
			
			if(serviceRowCount > 0) {
				//根据业务账号分组查询并统计每个业务账号的总时间放在右侧上方使用
				List<UserListenHistoryVo> serviceResults = customerOnDemandListenDao.selectListenHistoryCustomerOnDemandByStatisticalDimensionList(serviceMap);
				for(int i=0;i<serviceResults.size();i++){
					String  cod=serviceResults.get(i).getF_ulh_playtime();
					int code =0;
					boolean flag=true;
					for(int k=0;k<cod.length();k++){
						if (!Character.isDigit(cod.charAt(k))){
							flag=false;
						}
					}
					
					if(flag && !cod.equals("")){
						code =Integer.parseInt(serviceResults.get(i).getF_ulh_playtime());
					}else{
						code=0;
					}
					UserListenHistoryVo uv=serviceResults.get(i);
					uv.setF_ulh_play_minut((code/60)+"");
					uv.setF_ulh_playtime(code+"");
					int h=code/3600;
					int m=(code-3600*h)/60;
					int s=code%60;
					String strh="";
					String strm="";
					String strs="";
					if(h==0){
						strh="0";
					}else{
						strh=""+h;
					}
					if(m==0){
						strm="0";
					}else{
						strm=""+m;
					}
					if(s==0){
						strs="0";
					}else{
						strs=""+s;
					}
					
					uv.setF_ulh_play_hms(strh+"小时"+strm+"分"+strs+"秒");
					
					serviceResults.set(i,uv );
				}
				map.put("serviceRows", serviceResults);
				
				//根据第一个客户及该客户下的第一个业务账号查询该业务账号下的视频播放时间统计
				if(UVo.getF_ulh_servername()==null||"".equals(UVo.getF_ulh_servername())){
					contentMap.put("f_ulh_servername", serviceResults.get(0).getF_ulh_servername());
				}else{
					contentMap.put("f_ulh_servername", UVo.getF_ulh_servername());
				}
				
				
				Integer contentRowCount = customerOnDemandListenDao.selectListenHistoryCustomerOnDemandByStatisticalDimensionCount(contentMap);
				map.put("contentTotal", contentRowCount);
				
				if(contentRowCount > 0) {
					//根据视频分组查询并统计每个视频的总时间放在右侧下方使用
					List<UserListenHistoryVo> contentResults = customerOnDemandListenDao.selectListenHistoryCustomerOnDemandByStatisticalDimensionList(contentMap);
					for(int i=0;i<contentResults.size();i++){
						String  cod=contentResults.get(i).getF_ulh_playtime();
						int code =0;
						boolean flag=true;
						for(int k=0;k<cod.length();k++){
							if (!Character.isDigit(cod.charAt(k))){
								flag=false;
							}
						}
						
						if(flag && !cod.equals("")){
							code =Integer.parseInt(contentResults.get(i).getF_ulh_playtime());
						}else{
							code=0;
						}
						UserListenHistoryVo uv=contentResults.get(i);
						uv.setF_ulh_play_minut((code/60)+"");
						uv.setF_ulh_playtime(code+"");
						int h=code/3600;
						int m=(code-3600*h)/60;
						int s=code%60;
						String strh="";
						String strm="";
						String strs="";
						if(h==0){
							strh="0";
						}else{
							strh=""+h;
						}
						if(m==0){
							strm="0";
						}else{
							strm=""+m;
						}
						if(s==0){
							strs="0";
						}else{
							strs=""+s;
						}
						
						uv.setF_ulh_play_hms(strh+"小时"+strm+"分"+strs+"秒");
						
						contentResults.set(i,uv );
					}
					map.put("contentRows", contentResults);
					
				} else {
					map.put("contentRows", new ArrayList<UserListenHistoryVo>());
				}
				
				
			} else {
				map.put("serviceRows", new ArrayList<UserListenHistoryVo>());
			}
			
		} else {
			map.put("companyRows", new ArrayList<UserListenHistoryVo>());
		}
		return map;
	}
	
	
	
}
